#!/usr/bin/env python

import BugleChecks

def header_checks(env):
    '''
    Checks that will go into the global config.h, and are needed to determine
    what to put in header files.
    '''
    if env.GetOption('help') or env.GetOption('clean'):
        return env

    conf = Configure(env, custom_tests = BugleChecks.tests, config_h = '../../config.h')
    conf.Define('_POSIX_C_SOURCE', '200112L', 'Enable IEEE 1003.1-2001 functionality')
    return conf.Finish()

def internal_checks(env, features):
    '''
    Checks that will only affect C files in the platform directory.
    '''

    if env.GetOption('help'):
        return env

    conf = Configure(env, custom_tests = BugleChecks.tests, config_h = 'platform_config.h')
    # Get benefit of as many extensions as possible
    conf.Define('_XOPEN_SOURCE', '600')
    conf.Define('_GNU_SOURCE', '1')
    features['vasprintf'] = conf.CheckFunc('vasprintf')
    features['strdup'] = conf.CheckFunc('strdup')
    features['strndup'] = conf.CheckFunc('strndup')
    return conf.Finish()

Import('envs', 'targets', 'srcdir')

envs['host'].Append(CPPPATH = [srcdir, Dir('.')])
envs['host'].MergeFlags('-pthread -lrt')
envs['host'] = header_checks(envs['host'])

features = {}
platform_env = internal_checks(envs['host'].Clone(), features)

sources = [
    '../cosf_pass.c',
    '../gettime_posix.c',
    '../io_posix.c',
    '../isfinite_pass.c',
    '../isnan_pass.c',
    '../nan_strtod.c',
    '../process_linux.c',
    '../round_pass.c',
    '../sinf_pass.c',
    '../strndup_soft.c',
    '../threads_posix.c',
    '../vsnprintf_pass.c']
if features['strdup']:
    sources.append('../strdup_pass.c')
else:
    sources.append('../strdup_soft.c')
if features['vasprintf']:
    sources.append('../vasprintf_pass.c')
else:
    sources.append('../vasprintf_vsnprintf.c')
targets['bugleutils']['source'].extend(platform_env.SharedObject(source = sources))

targets['bugle']['LIBS'].append('dl')
targets['bugle']['source'].extend(platform_env.SharedObject(source = [
    '../dl_dlopen.c',
    'dlopen.c']))
